package www.wen.com.common.lib.util

import android.content.Context
import www.wen.com.common.lib.app.BaseApplication
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader

/**
 * @author WEN
 * @Description:
 * @date 2021/6/22 10:31
 */
object ReadJsonFileUtil {

    /**
     * 得到json文件中的内容
     * @param context
     * @param fileName
     * @return
     */
    fun readJsonFile(fileName: String,
                     context: Context = BaseApplication.CONTEXT): String {
        val stringBuilder = StringBuilder()
        val assetManager = context.assets
        try {
            //使用IO流读取json文件内容
            val bufferedReader = BufferedReader(InputStreamReader(assetManager.open(fileName), "utf-8"))
            var line: String?
            while (bufferedReader.readLine().also { line = it } != null) {
                stringBuilder.append(line)
            }
            bufferedReader.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return stringBuilder.toString()
    }

}